package com.sharing.service;

import com.sharing.common.*;
import com.sharing.domain.User;
import com.sharing.domain.UserProduction;
import com.sharing.mapper.UserMapper;
import com.sharing.mapper.UserProductionMapper;
import com.sharing.util.ThreadLocalStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.w3c.dom.CharacterData;

import java.util.List;
/**
 * @author Du
 * @version 1.0
 * @description TODO
 * @date 2023/4/4 21:39
 */
@Service
public class UserProductionService {
    @Autowired
    private UserProductionMapper userProductionMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户id查询该用户所有的作品
     * @param userId
     * @return
     */
    public Result<Page<UserProduction>> selectContentByUserId(Integer userId, Integer page, Integer size) {
        User loginUser = ThreadLocalStorage.getUser();
        if (loginUser == null) {
            synchronized (this) {
                if (loginUser == null) {
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    loginUser = userMapper.selectUserByUserName(authentication.getName());
                    ThreadLocalStorage.setUser(loginUser);
                }
            }
        }
        if (userId != null && loginUser.getId() != userId) {
            return new Result<>(1, "传入参数有误");
        }
        userId = loginUser.getId();

        // 查询该用户作品总数
        Integer total = userProductionMapper.selectProductionsNumByUserId(userId);

        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = total;
        }

        List<UserProduction> userProductions = userProductionMapper.selectProductionsByUserId(userId, (page - 1) * size, size);

        return new Result<>(0, new Page<UserProduction>(total, size, userProductions));
    }

    /**
     * 根据id删除作品
     * @param id
     * @return
     */
    public Result<Integer> deleteContent(Integer id) {
        UserProduction userProduction = userMapper.selectUserProductionById(id);
        if (userProduction == null) {
            return new Result<>(1, "传入参数不当");
        }

        User loginUser = ThreadLocalStorage.getUser();
        if (loginUser == null) {
            synchronized (this) {
                if (loginUser == null) {
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    loginUser = userMapper.selectUserByUserName(authentication.getName());
                    ThreadLocalStorage.setUser(loginUser);
                }
            }
        }

        if (loginUser.getId() != userProduction.getUserId()) {
            return new Result<>(1, "该用户没有删除该作品的权限");
        }

        return new Result<>(0,userProductionMapper.deleteContent(id));
    }

    /**
     * 更新作品
     * @param userProduction
     * @return
     */
    public Result<Integer> updateContent(UserProduction userProduction) {
        //校验内容是否合法
        String content = userProduction.getContent();
        if(content == "" || content == null) {
            return new Result<>(1,"内容不合法，请重新更改");
        }

        User loginUser = ThreadLocalStorage.getUser();
        if (loginUser == null) {
            synchronized (this) {
                if (loginUser == null) {
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    loginUser = userMapper.selectUserByUserName(authentication.getName());
                    ThreadLocalStorage.setUser(loginUser);
                }
            }
        }

        if (loginUser.getId() != userProduction.getUserId()) {
            return new Result<>(1, "该用户没有修改作品的权限");
        }

        return new Result<>(0, userProductionMapper.updateContentById(userProduction));
    }

    /**
     * 新增作品
     * @param userProduction
     * @return
     */
    public Result<UserProduction> insertContent(UserProduction userProduction) {
        //校验内容是否合法
        String content = userProduction.getContent();
        if(content == "" || content == null) {
            return new Result<>(1,"内容不合法，请重新更改");
        }

        User loginUser = ThreadLocalStorage.getUser();
        if (loginUser == null) {
            synchronized (this) {
                if (loginUser == null) {
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    loginUser = userMapper.selectUserByUserName(authentication.getName());
                    ThreadLocalStorage.setUser(loginUser);
                }
            }
        }

        userProduction.setUserId(loginUser.getId());
        UserProduction userProduction1 = userProductionMapper.insertContent(userProduction);

        return new Result<>(0, userProduction1);
    }

    public Result<UserProduction> selectUserProductionById(Integer id) {
        UserProduction userProduction = userProductionMapper.selectProductionById(id);

        if (userProduction == null) {
            return new Result<>(1, "该作品不存在");
        }
        return new Result<>(0, userProduction);
    }

    public Result<List<UserProduction>> selectUserProductionsByIds(List<Integer> productionIds) {
        if (productionIds == null) return new Result<>(1, "参数为空");

        List<UserProduction> users = userProductionMapper.selectProductionsByIds(productionIds);

        return new Result<>(0, users);
    }
}
